using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.Sales
{
    [Authorize]
    public class IntentOrderTypeAppService : BasicArchivesAppService, IIntentOrderTypeAppService
    {
        protected IIntentOrderTypeRepository IntentOrderTypeRepository => LazyServiceProvider.LazyGetRequiredService<IIntentOrderTypeRepository>();
        protected IntentOrderTypeManager IntentOrderTypeManager => LazyServiceProvider.LazyGetRequiredService<IntentOrderTypeManager>();

        [Authorize(BasicArchivesPermissions.IntentOrderType.Query)]
        public async Task<IntentOrderTypeDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<IntentOrderType, IntentOrderTypeDto>(
                await IntentOrderTypeRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.IntentOrderType.Query)]
        public async Task<IntentOrderTypeDto> FindAsync(string filter)
        {
            var entity = await IntentOrderTypeRepository.FindAsync(x => x.DisplayName == filter);
            return ObjectMapper.Map<IntentOrderType, IntentOrderTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.IntentOrderType.Query)]
        public async Task<IntentOrderTypeDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await IntentOrderTypeRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<IntentOrderType, IntentOrderTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.IntentOrderType.Query)]
        public async Task<PagedResultDto<IntentOrderTypeDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await IntentOrderTypeRepository.GetCountAsync(input.Filter, true);
            var list = await IntentOrderTypeRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<IntentOrderTypeDto>(
                totalCount,
                ObjectMapper.Map<List<IntentOrderType>, List<IntentOrderTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.IntentOrderType.Query)]
        public async Task<PagedResultDto<IntentOrderTypeDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<IntentOrderType, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<IntentOrderType>(input.FilterGroup);
            }

            var totalCount = await IntentOrderTypeRepository.GetCountAsync(expression, true);
            var list = await IntentOrderTypeRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<IntentOrderTypeDto>(
                totalCount,
                ObjectMapper.Map<List<IntentOrderType>, List<IntentOrderTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.IntentOrderType.Create)]
        public async Task<IntentOrderTypeDto> CreateAsync(IntentOrderTypeCreateDto input)
        {
            var entity = ObjectMapper.Map<IntentOrderTypeCreateDto, IntentOrderType>(input);

            await IntentOrderTypeManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.IntentOrderType.Update)]
        public async Task<IntentOrderTypeDto> UpdateAsync(Guid id, IntentOrderTypeUpdateDto input)
        {
            var entity = await IntentOrderTypeRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await IntentOrderTypeManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.IntentOrderType.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await IntentOrderTypeManager.DeleteAsync(id);
        }
    }
}